 set more off
use "C:\Users\Andy baker\OneDrive - UCB-O365\Data\Brazil\BEPS 2014\BEPS 2014 Merged Data v3.dta", clear

xtset idtelefone wave
gen respond=1

*Before reshaping, generate some variables
	*vote choice
recode vb1 1=1 2=2 3=3 4=4 5/12=5 13/14=6 .a=6 .b=6, gen(vote)
label define vote 1 Dilma 2 Aecio 3 Campos 4 Marina 5 Other 6 "Abstain/NR"
label values vote vote

recode vote 1=1 2/14=0 else=., gen(vote_dilma)
recode vote 2=1 1=0 3/14=0 else=., gen(vote_neves)
recode vote 3/4=1 1/2=0 5/14=0 else=., gen(vote_campos)
recode vote 5/6=1 1/4=0 else=., gen(vote_base)

*Number of discussants
	*Number of discussants
replace disc5b=. if disc5a==.a | disc5a==.b | disc5a==.c | disc5a==.
replace disc6b=. if disc6a==.a | disc6a==.b | disc6a==.c | disc6a==.

recode disc5a 1/8=1 9/10=0, gen(disc_family)
recode disc6a 1/8=1 10=0, gen(disc_friend)
egen disc_total=rsum(disc_family disc_friend)
replace disc_total=. if wave==1 | wave==6 | wave==7

	*Discussion frequency
recode disc1-disc3 (1=4) (2=3) (3=2) (4=1) (.=.)
rename disc1 discfam
rename disc2 discfriends

	*Media exposure
recode med1 (1=45) (2=30) (3=14) (4=4) (5=.5), gen(mediaattention)


		*demographics
rename mulher woman
recode soct1 (1=5) (2=4) (3=3) (4=2) (5=1), gen(socio_current)
recode soct2 (1=3) (2=2) (3=1), gen(socio_change)
recode idio1 (1=5) (2=4) (3=3) (4=2) (5=1), gen(self_current)
recode spp1 2=0, gen(bf)
recode spp2 2=0, gen(tarifa_social)

recode pol1 (1=4) (2=3) (3=2) (4=1), gen(interest)
gen age=ln(p1_1)
recode urb_rur 2=0 1=1, gen(urban)

recode p2 1=1 .=. .a=.a .b=.b else=0, gen(white)
recode p2 2=1 .=. .a=.a .b=.b else=0, gen(brown)
recode p2 3=1 .=. .a=.a .b=.b else=0, gen(indigenous)
recode p2 4=1 .=. .a=.a .b=.b else=0, gen(black)
recode p2 5=1 .=. .a=.a .b=.b else=0, gen(asian)
replace educ=ln(educ)

recode p2 1=3 2=2 3=1 4=1 5=2, gen(racemean)

drop wealth
recode hw1 hw2 hw3 hw4 hw5 hw6 p16 (2=0)
irt grm hw* p16 if wave==1
predict wealth if wave==1, latent ebmeans

		*issues: social issues not really working here
polychoricpca soc11 soc7 pp10 pp11 pp12 pp13 pp14, score(econ_issues) nscore(1)
impute econ_issues soc11 soc7 pp10 pp11 pp12 pp13 pp14 if wave==1, gen(econ_issues)

*polychoricpca abort rac2b d6b mil1, score(social_issues) nscore(1)
*rename social_issues1 social_issues

rename atual1 petrobras

*Remember that any vars you want to use must be put in the next two commands
keep idtelefone wave respond vote disc_* discfam discfriends mediaattention p15 disc5a disc6a woman m1 l1 socio_current socio_change self_current bf tarifa_social white brown indigenous black asian educ wealth econ_issues vote_dilma vote_campos vote_neves vote_base interest urban age weight_combined petrobras p4 pk1 pk2 pk3 pk4 pk5 ft1 ft2 ft3 ft4 ft5 ft6 ft7 racemean p5
reshape wide respond vote disc_* discfam discfriends mediaattention p15 bf disc5a disc6a m1 l1 socio_current socio_change self_current tarifa_social white brown indigenous black asian educ wealth econ_issues vote_dilma vote_campos vote_neves vote_base interest urban age weight_combined petrobras p4 pk1 pk2 pk3 pk4 pk5 ft1 ft2 ft3 ft4 ft5 ft6 ft7 racemean  p5, i(idtelefone) j(wave)

gen weight_combined45=weight_combined4
replace weight_combined45=weight_combined5 if respond5==1


*Of all opinionated voters (does not include DK in the denominator, and also excludes none/abstention or nr)
*All combos: 1 to 2, 1 to 3, 1 to 4, 1 to 5, 1 to 6, 2 to 4, 2 to 5, 2 to 6, 3 to 4, 3 to 5, 3 to 6, 4 to 6, 5 to 6.
			*first wave Marina voters (but not Campos voters) recoded to .
gen change12=0 if respond1==1 & respond2==1 & vote1~=4 & vote1~=17 & vote1~=16 & vote1~=11 & vote1~=. & vote2~=17 & vote2~=16 & vote2~=6 & vote2~=.
replace change12=1 if vote1~=vote2 & vote1<16 & vote2<16 & change12==0
bysort change12: tab vote1 vote2 if respond2==1, m

				*Campos died between waves 2 and 3 on 8/13
			*first and third wave Campos voters recoded to .
gen change13=0 if respond1==1 & respond3==1 & vote1~=3 & vote1~=17 & vote1~=16 & vote1~=11 & vote1~=. & vote3~=3 & vote3~=17 & vote3~=16 & vote3~=.
replace change13=1 if vote1~=vote3 & vote1<16 & vote3<16 & change13==0
bysort change13: tab vote1 vote3 if respond3==1, m

egen change123=rsum(change12 change13)
replace change123=. if change12==. & change13==. 

gen change14=0 if respond1==1 & respond4==1 & vote1~=3 & vote1~=17 & vote1~=16 & vote1~=11 & vote1~=. & vote4~=7 & vote4~=6 & vote4~=17 & vote4~=16 & vote4~=.
replace change14=1 if vote1~=vote4 & vote1<16 & vote4<16 & change14==0
bysort change14: tab vote1 vote4 if respond4==1, m

gen change15=0 if respond1==1 & respond5==1 & vote1~=3 & vote1~=17 & vote1~=16 & vote1~=11 & vote1~=. & vote5~=7 & vote5~=6 & vote5~=17 & vote5~=16 & vote5~=.
replace change15=1 if vote1~=vote5 & vote1<16 & vote5<16 & change15==0
bysort change15: tab vote1 vote5 if respond5==1, m

			*second wave Campos voters recoded to .
gen change24=0 if respond2==1 & respond4==1 & vote2~=3 & vote2~=17 & vote2~=16 & vote2~=. & vote4~=17 & vote4~=16 & vote4~=.
replace change24=1 if vote2~=vote4 & vote2<16 & vote4<16 & change24==0
bysort change24: tab vote2 vote4, m

 gen change25=0 if respond2==1 & respond5==1 & vote2~=3 & vote2~=17 & vote2~=. & vote5~=17 & vote5~=. & vote2~=16 & vote5~=16
replace change25=1 if vote2~=vote5 & vote2<16 & vote5<16 & change25==0
bysort change25: tab vote2 vote5, m

gen change26=0 if respond2==1 & respond6==1 & vote2~=3 & vote2~=17 & vote2~=. & vote2~=16 & vote6~=17 & vote6~=. & vote6~=16
replace change26=1 if vote2~=vote6 & vote2<16 & vote6<16 & change26==0
bysort change26: tab vote2 vote6, m

gen change16=0 if respond1==1 & respond6==1 & vote1~=3 & vote1~=17 & vote1~=11 & vote1~=16 & vote1~=16 & vote1~=. & vote6~=17 & vote6~=. & vote6~=16
replace change16=1 if vote1~=vote6 & vote1<16 & vote6<16 & change16==0
bysort change16: tab vote1 vote6 if respond6==1, m

*--------------- choice set remains the same after wave 2, so things get easier
gen change34=0 if respond3==1 & respond4==1 & vote3~=17 & vote3~=. & vote4~=17 & vote4~=. & vote3~=16 & vote4~=16
replace change34=1 if vote3~=vote4 & vote3<16 & vote4<16 & change34==0
bysort change34: tab vote3 vote4, m

gen change35=0 if respond3==1 & respond5==1 & vote3~=17 & vote3~=. & vote5~=17 & vote5~=. & vote3~=16 & vote5~=16
replace change35=1 if vote3~=vote5 & vote3<16 & vote5<16 & change35==0
bysort change35: tab vote3 vote5, m

egen change2345=rsum(change24 change25 change34 change35)
replace change2345=. if change24==. & change25==. & change34==. & change35==.

gen change36=0 if respond3==1 & respond6==1 & vote3~=17 & vote3~=. & vote3~=16 & vote6~=17 & vote6~=. & vote6~=16
replace change36=1 if vote3~=vote6 & vote3<16 & vote6<16 & change36==0
bysort change36: tab vote3 vote6, m

gen change46=0 if respond4==1 & respond6==1 & vote4~=17 & vote4~=. & vote4~=16  & vote6~=17 & vote6~=. & vote6~=16
replace change46=1 if vote4~=vote6 & vote4<16 & vote6<16 & change46==0
bysort change46: tab vote4 vote6, m

gen change56=0 if respond5==1 & respond6==1 & vote5~=17 & vote5~=. & vote6~=17 & vote6~=.  & vote5~=16 & vote6~=16
replace change56=1 if vote5~=vote6 & vote5<16 & vote6<16 & change56==0
bysort change56: tab vote5 vote6, m

egen change456=rsum(change46 change56)
replace change456=. if change46==. & change56==. 


*DISCUSSANT IVS
*Discussion propensity
egen conversemean=rowmean(discfam1 discfriends1)
egen mediaattention=rowmean(mediaattention2 - mediaattention6)
egen disctotalmean=rowmean( disc_total2 disc_total3 disc_total4 disc_total5)

	*I added in ideo and ft because the set of pk items is severely truncated at the bottom. 43% didn't know any.
recode pk11 pk21 pk31 pk41 pk51 (.a=0) (.b=0) (2=0)
recode l11 (0/10=1) (.a=0) (.b=0) (.=.), gen(ideo_respond1) 
recode ft11-ft71 (0/10=1) (.a=0) (.b=0) (.=.)
egen ft_know=anycount(ft11 ft21 ft31 ft41 ft51 ft61 ft71), values(1)
irt grm pk11 pk21 pk31 pk41 pk51 ideo_respond1 ft_know [pweight=weight_combined1] 
predict aware_irt, latent ebmeans
egen awarez=std(aware_irt)

*Gen correct voting Score
	*Valence
recode l11 0=1 1=.8 2=.6 3=.4 4=.2 5=0 6=-.2 7=-.4 8=-.6 9=-.8 10=-1, gen(ideo1) 
recode socio_current1 1=-1 2=-.5 3=0 4=.5 5=1 6=., gen(socio1)
recode socio_change1 3=1 2=0 1=-1, gen(socio_c1)
*recode self_current1 1=-1 2=-.5 3=0 4=.5 5=1 6=., gen(pocket1)
recode m11 1=1 2=.5 3=0 4=-.5 5=-1, gen(dilma_approve1)

	*Positional
summ econ_issues1
replace econ_issues1= ((econ_issues1-r(min))/(r(max)-r(min)))
replace econ_issues1=2*econ_issues1-1
gen benefits1=(bf1+tarifa_social1)-1
*recode bf1 1=1 0=-1
recode petrobras1 3=1 2=0 1=-1
recode p51 1=4 2=3 3=2 4=1 5=0, gen(evangelical1)
replace evangelical1=0 if p41==1|p41>3
recode evangelical1 4=1 3=.5 2=0 1=-.5 0=-1

*from W-B, tax vs spend
display -1.1052+.1052*5.97 /*PT*/
display -1.1052+.1052*13.027  /*PSDB*/
display -1.1052+.1052*7.2 /*PSB*/

*from W-B, ideology
display -1.1052+.1052*6.37 /*PT*/
display -1.1052+.1052*13.46  /*PSDB*/
display -1.1052+.1052*7.5 /*PSB*/

capture program drop cv
program define cv
while "`1'" ~= "" {
gen dilma_`1'=-(abs(`1'-`2')-1)
gen neves_`1'=-(abs(`1'-`3')-1)
gen marina_`1'=-(abs(`1'-`4')-1)
mac shift
mac shift
mac shift
mac shift
}
exit
end

cv socio1 1 -1 -1 socio_c1 1 -1 -1 dilma_approve1 1 -1 -1
cv econ_issues1 -.477 .26 -.3476
cv petrobras1 1 -1 -1
cv evangelical1 -1 -1 1
cv benefits1 -.477 .26 -.3476 
cv ideo1 -.435076 .310792 -.3162 /*oddly, ideological self-placement just didn't matter, so not in CV calculation*/

egen dilma1_av=rowtotal(dilma_socio1 dilma_dilma_approve1 dilma_econ_issues1 dilma_petrobras1 dilma_evangelical1  )
egen neves1_av=rowtotal(neves_socio1 neves_dilma_approve1 neves_econ_issues1 neves_petrobras1 neves_evangelical1 )
egen marina1_av=rowtotal(marina_socio1 marina_dilma_approve1 marina_econ_issues1 marina_petrobras1 marina_evangelical1)

gen correct_vote1=1 if dilma1_av>neves1_av & dilma1_av>marina1_av
replace correct_vote1=2 if neves1_av>dilma1_av & neves1_av>marina1_av
replace correct_vote1=4 if marina1_av>neves1_av & marina1_av>dilma1_av

recode vote1 3=4
gen voted_correctly1to1=1 if correct_vote1==vote1 & vote1<5
replace voted_correctly1to1=0 if correct_vote1~=vote1 & vote1<5

gen voted_correctly1to6=1 if correct_vote1==vote6 & vote6<5
replace voted_correctly1to6=0 if correct_vote1~=vote6 & vote6<5

gen voted_correctly1to45=1 if correct_vote1==vote4 & vote4<5
replace voted_correctly1to45=0 if correct_vote1~=vote4 & vote4<5
replace voted_correctly1to45=1 if correct_vote1==vote5 & vote5<5
replace voted_correctly1to45=0 if correct_vote1~=vote5 & vote5<5

gen voted_correctly1to3=1 if correct_vote1==vote3 & vote3<5
replace voted_correctly1to3=0 if correct_vote1~=vote3 & vote3<5

	*Static Stuff
*Figure 8.4, Table C.1
eststo clear
eststo: logit voted_correctly1to6 awarez i.vote6 [pweight=weight_combined1]
margin, at((p5) awarez) at((p95) awarez) contrast(atcontrast(r)) atmeans

eststo: logit voted_correctly1to6 conversemean mediaattention i.vote6 [pweight=weight_combined1]
margin, at((p5) conversemean) at((p95) conversemean) contrast(atcontrast(r)) atmeans
margin, at((p5) mediaattention) at((p95) mediaattention) contrast(atcontrast(r)) atmeans

logit voted_correctly1to6 disctotalmean mediaattention i.vote6 [pweight=weight_combined1]

eststo: logit voted_correctly1to6 wealth1 educ1 woman racemean1 i.vote6 [pweight=weight_combined1]
margin, at((p5) wealth1 educ) at((p95) wealth1 educ) contrast(atcontrast(r)) vce(unconditional) atmeans

cd "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 8 Normative\Output\"
esttab est1 est2 est3 using Brazil2014CV.rtf, label b(3) replace se star(* 0.05 ) nogap onecell title(CV Brazil 14)
xyz

*Table 8.1
	*Change Stuff
logit voted_correctly1to6 voted_correctly1to3 change36 i.vote6 [pweight=weight_combined1]
logit voted_correctly1to6 voted_correctly1to45 change456 i.vote6 [pweight=weight_combined1]

logit voted_correctly1to6 voted_correctly1to3 conversemean i.vote6 [pweight=weight_combined1]
logit voted_correctly1to6 voted_correctly1to45 conversemean i.vote6 [pweight=weight_combined1]

svyset [pweight=weight_combined1]

svy: tab voted_correctly1to1 
svy: tab voted_correctly1to45 
svy: tab voted_correctly1to6 

*svy: tab voted_correctly1to1 if voted_correctly1to45~=. & voted_correctly1to6~=.
svy: tab voted_correctly1to45 if voted_correctly1to6~=. 
svy: tab voted_correctly1to6 if voted_correctly1to45~=.
